using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ObjectLibrary;

namespace _0912089_0912172
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TrailerScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private My2DVideo videoTrailer2D;
        private MyCamera camera;
        private BasicEffect effect;

        private int height;
        private int width;
        private bool isEscapeKeyPressed = false;
        private SpriteBatch spriteBatch;

        public TrailerScreen(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            height = game.Window.ClientBounds.Height;
            width = game.Window.ClientBounds.Width;
            //Initialize();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();

            camera = new NormalPerspectiveCamera();

            ((NormalPerspectiveCamera)camera).initCamera(
                new Vector3(0, 0, 10.3f),
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0),
                1,
                200,
                MathHelper.PiOver4,
                width / height);

            effect = new BasicEffect(GraphicsDevice);

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            videoTrailer2D = new My2DVideo(Game.Content, @"Resources/Videos/trailer", new Rectangle(0, 0, width, height));
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);

            videoTrailer2D.Play();
            videoTrailer2D.Update(gameTime);
            if (videoTrailer2D.isStop())
            {
                Game.Components.RemoveAt(0);
                Global.gameMode = Global.GAME_MODE.MAIN_MENU;
                Game.Components.Add(new MenuScreen(Game));
            }
            KeyboardState kbs = Keyboard.GetState();
            if (kbs.IsKeyDown(Keys.Escape))
            {
                isEscapeKeyPressed = true;
            }
            if (kbs.IsKeyUp(Keys.Escape) && isEscapeKeyPressed)
            {
                isEscapeKeyPressed = false;
                videoTrailer2D.Stop();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkCyan);
            spriteBatch.Begin();
            videoTrailer2D.Draw(gameTime, spriteBatch);
            spriteBatch.End();

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
            base.Draw(gameTime);
        }
    }
}
